package poc.android.model;

import java.util.Map;

import org.json.JSONObject;


/**
 * 
 * @author Jason
 *
 */
public class School extends AbstractJSonModel {
	
	/**
	 * returns a default set of schools (only 2)
	 */
	public static School[] availableSchools=new School[2];
	static {
		availableSchools[0]=new School();
		availableSchools[0].id=Integer.valueOf(1);
		availableSchools[0].name="King's Primary";
		availableSchools[0].address="King's Road North 101 avenue";
		
		availableSchools[1]=new School();
		availableSchools[1].id=Integer.valueOf(2);
		availableSchools[1].name="Quebery Intermediate";
		availableSchools[1].address="UK 128 Eilnade Road";
	}
	
	Integer id;
	String name;
	String address;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public JSONObject toJSon(Map<String, Object> pConfig) {
		JSONObject aJObj=null;
		
		if (this.id!=null) {
			try {
				aJObj=new JSONObject();
				aJObj.put(ID_FIELD, this.getClass().getCanonicalName());
				aJObj.put("id", this.id);
				aJObj.put("name", this.name);
				aJObj.put("address", this.address);
				
			} catch(Exception e) {
				e.printStackTrace();
				throw new RuntimeException("[School][toJSon]", e);
			}
		}
		return aJObj;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public static Object toObjectFromJSonString(String pJSonString) {
		Object aObj=null;
		
		try {
			if (pJSonString!=null && pJSonString.trim().length()>0) {
				JSONObject aJObj=new JSONObject(pJSonString);
				aObj=School.toObjectFromJSonObject(aJObj);
			}
		} catch(RuntimeException re) {
			throw re;
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("[School][toObjectFromJSonString]", e);
		}
		return aObj;
	}
	/**
	 * {@inheritDoc}
	 */
	public static Object toObjectFromJSonObject(JSONObject pJSonObject) {
		School aObj=null;
		
		if (pJSonObject!=null) {
			String aVal=null;
			
			try {
				aVal=pJSonObject.getString(ID_FIELD);
				if (aVal!=null && aVal.equals(School.class.getCanonicalName())) {
					aObj=new School();
					aObj.setId(Integer.valueOf(pJSonObject.getString("id")));
					aObj.setName(pJSonObject.getString("name"));
					aObj.setAddress(pJSonObject.getString("address"));
				}
			} catch(Exception e) {
				e.printStackTrace();
				throw new RuntimeException("[School][toObjectFromJSonObject]", e);
			}
		}
		return aObj;
	}
}
